package com.first.nell.components.form

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.first.nell.R
import com.first.nell.data.DataWalletChannelFcfa
import com.first.nell.extend.isFirstNellLink


class FcfaSelectWalletAdapter(val dataList: List<DataWalletChannelFcfa>) :
    RecyclerView.Adapter<FcfaSelectWalletAdapter.ViewHolder>() {

    private var mFirstNellOnFirstNellItemFirstNellClickFirstNellListener: (Int, DataWalletChannelFcfa) -> Unit =
        { positon, product -> }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tv_sim_type_list_title: TextView =
            view.findViewById(R.id.tv_sim_type_list_title)
        val iv_sim_type_list_logo: ImageView =
            view.findViewById(R.id.iv_sim_type_list_logo)
        val tv_sim_type_list_detail: TextView =
            view.findViewById(R.id.tv_sim_type_list_detail)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_first_nell_adpter_sim_type, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: DataWalletChannelFcfa = dataList[position]
        holder.tv_sim_type_list_title.text = data.desc
        holder.tv_sim_type_list_detail.text = data.detail

        holder.tv_sim_type_list_title.setOnClickListener {
            mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
        }

        holder.tv_sim_type_list_detail.setOnClickListener {
            mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
        }

        holder.iv_sim_type_list_logo.setOnClickListener {
            mFirstNellOnFirstNellItemFirstNellClickFirstNellListener.invoke(position, data)
        }

        if (data.logo.toString().isFirstNellLink()) {
            holder.iv_sim_type_list_logo.visibility = View.VISIBLE
            Glide.with(holder.iv_sim_type_list_logo.context).load(data.logo)
                .into(holder.iv_sim_type_list_logo)
        } else {
            holder.iv_sim_type_list_logo.visibility = View.GONE
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    fun setFirstNellOnFirstNellItemFirstNellClickFirstNellListener(listener: (Int, DataWalletChannelFcfa) -> Unit) {
        mFirstNellOnFirstNellItemFirstNellClickFirstNellListener = listener
    }

}